package org.com.system.user.service;



import java.util.Iterator;
import java.util.Set;

import org.com.system.user.pojo.Role;
import org.com.system.user.pojo.User;
import org.com.system.util.dao.HibernateDao;
import org.com.system.util.model.Service;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository("userServer")
@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
public class UserService extends HibernateDao<User> implements Service{
	
	/**
	 * 为指定用户添加角色。
	 * @param uid
	 * @param rid
	 * @param opt
	 */
	public void addRole(String uid,long rid,String opt){
		Session session=getCurrentSession();
		Role role=(Role)session.get(Role.class, rid);
		User user=(User)session.get(User.class,uid);
		user.getRoles().add(role);
		session.saveOrUpdate(user);
	}
	
	/**
	 * 为指定用户删除角色。
	 * @param uid
	 * @param rid
	 */
	public void removeRole(String uid,long rid){
		Session session=getCurrentSession();
		User user=(User)session.get(User.class,uid);
		Set<Role> set=user.getRoles();
		Iterator<Role> it=set.iterator();
		Role role=null;
		while(it.hasNext()){
			role=it.next();
			if(rid==role.getId()){
				break;
			}
		}
		set.remove(role);
		session.saveOrUpdate(user);
	}
	
	/**
	 * 取得所有权限的详细信息。
	 * @param uid
	 * @return
	 */
	public String authDetail(String uid){
		
		return null;
	}
	
}